import 'package:flutter/material.dart';
import 'package:get/get.dart';

import 'Test_class_status.dart';

class Test_status extends StatelessWidget {
  Test_status({super.key});

  RxInt _counter = 0.obs;
  final _counter2 = RxString('123');
  final bool_v = RxBool(true);
  final double_v = RxDouble(0.2);
  final list_v = RxList<int>([1, 2, 3]);
  final list_person = RxList<Person>(
      [Person('jack1', 12, Bag('123')), Person('jack2', 13, Bag('124'))]);
  final map_v = RxMap<String, int>({});
  final int_v = RxInt(123);
  RxList<Person> person_list = RxList<Person>([]);

  @override
  StatelessElement createElement() {
    // TODO: implement createElement
    //  开始监听
    var listenr = int_v.listen((v) {
      print('JtTag:Test_status:监听到变化:V：${v}');
    });
    // 取消监听
    listenr.cancel();
    return super.createElement();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Test_status')),
        body: Column(children: [
          // RxInt 类型
          Obx(() => Center(child: Text('$_counter'))),
          ElevatedButton(
              onPressed: () {
                _counter++;
              },
              child: Text('+')),
          ElevatedButton(
              onPressed: () {
                _counter--;
              },
              child: Text('-')),
          // --------------------------------------------------------RxString 类型
          Obx(() => Text('$_counter2')),
          ElevatedButton(
              onPressed: () {
                _counter2.value += '1';
              },
              child: Text('+')),
          ElevatedButton(
              onPressed: () {
                _counter2.value = _counter2.substring(1);
              },
              child: Text('-')),
          Obx(() => Text('$int_v')),
          // --------------------------------------------------------int 类型
          ElevatedButton(
              onPressed: () {
                int_v.value++;
              },
              child: Text('+')),
          ElevatedButton(
              onPressed: () {
                int_v.value--;
              },
              child: Text('-')),
          Row(
              children: List.generate(list_v.length, (index) {
            return TextButton(
                onPressed: () {
                  list_v[index]++;
                },
                child: ObxValue((data) {
                  return Text(list_v[index].toString());
                }, list_v));
          })),
          Column(
            children: List.generate(list_person.length, (index) {
              return TextButton(
                  onPressed: () {
                    list_person[index].age++;
                    list_person[index] = list_person[index];
                  }, child: ObxValue((data){
                    return Text(list_person[index].toString());
                  }, list_person));
            }),
          )
        ]));
  }
}
